package com.jzy.secondhandbooktradingmanagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzy.secondhandbooktradingmanagement.commom.Result;
import com.jzy.secondhandbooktradingmanagement.entity.Book;
import com.jzy.secondhandbooktradingmanagement.service.IBookService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    IBookService bookService;

    @PostMapping
    public Result<?> save(@RequestBody Book Book){
        bookService.saveBook(Book);
        return Result.success();
    }
    @PutMapping
    public  Result<?> update(@RequestBody Book Book){
        bookService.updateBook(Book);
        return Result.success();
    }

    //    批量删除
    @PostMapping("/deleteBatch")
    public  Result<?> deleteBatch(@RequestBody List<Integer> ids){
        bookService.removeBatchByIds(ids);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        bookService.removeById(id);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search1", defaultValue = "") String search1,
                              @RequestParam(name = "search2", defaultValue = "") String search2,
                              @RequestParam(name = "search3", defaultValue = "") String search3){
        Page<Book> BookPage = bookService.findPage(pageNum, pageSize, search1, search2, search3);
        return Result.success(BookPage);
    }
}
